#include <cstdio>
#include <iostream>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 1e6+10;
int ans[maxn];
int flag[maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int p;
		scanf("%d",&p);
		if(p==2) printf("1\n");
		else 
		{
			for(int i = 0;i<=p;i++) flag[i] = 0;
			int vis = 0;
			flag[1] = 1;
			ans[1] = 1;
			int cnt  = 1;
			while(cnt<p-1)
			{
				if(flag[ans[cnt]*2%p]==0)
				{
					cnt++;
					ans[cnt] = ans[cnt-1]*2%p;
					flag[ans[cnt]] = 1;
				//	printf("+++%d\n",ans[cnt]);
				}
				else if(flag[ans[cnt]*3%p]==0)
				{
					cnt++;
					ans[cnt] = ans[cnt-1]*3%p;
					flag[ans[cnt]] = 1;
				//	printf("***%d\n",ans[cnt]);
				}
				else 
				{
					vis = 1;
					break;
				}
			}
			if(vis) printf("-1\n");
			else 
			{
				printf("%d",ans[1]);
				for(int i = 2;i<=p-1;i++) printf(" %d",ans[i]);
				printf("\n");
			}
		}
	}
} 
